/*
 * 文件名：OutcaeSureController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月21日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.mgr.bpa.domain.OutcaeSureAssist;
import top.infopub.mgr.bpa.domain.OutcaeSureDomain;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.bpa.vo.OutcaeSureSubVo;
import top.infopub.mgr.bpa.vo.OutcaeSureVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.mgr.util.FileHelper;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.bpa.OutcaeSureAtt;
import top.infopub.model.bpa.OutcaeSureDetail;
import top.infopub.model.bpa.OutcaeSureSub;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;


/**确权支付
 * @author wuzhiyang
 * @version 2020年2月21日
 * @see OutcaeSureController
 * @since商务管理/确权支付/对外建安产值确权
 */
@Controller
@RequestMapping("/outcaeSure")
public class OutcaeSureController extends GenericFlowController {

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private ContractExtService contractExtService;

    @Autowired
    private ContractExtDetailService contractExtDetailService;

    @Autowired
    private ProContractDetailService proContractDetailService;

    @Autowired
    private OutcaeSureDetailService outcaeSureDetailService;

    @Autowired
    private ContractExtInvestService contractExtInvestService;

    @Autowired
    private ProContractInvestService proContractInvestService;

    @Autowired
    private InnerAgrtDetailService innerAgrtDetailService;

    @Autowired
    private SubpackCotDetailService subpackCotDetailService;

    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * 对外建安产值确权列表
     * @return 
     * @see
     */
    @RequestMapping("/list")
    public String list() {
        return "bpa/lay-outcaeSure-list";
    }

    /**
     * 对外建安产值确权列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<OutcaeSureDomain> queryDataByCondition(OutcaeSureVo outcaeSureVo,
                                                        ParamPage paramPage,
                                                        String monthStartDateStr,
                                                        String monthEndDateStr) {
        Page<OutcaeSureDomain> page = new Page<OutcaeSureDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (outcaeSureVo.getEndDate() != null) {
            outcaeSureVo.setEndDate(DateUtils.addDays(outcaeSureVo.getEndDate(), 1));
        }
        //查询时，确权开始日期
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            outcaeSureVo.setStartSureDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，确权结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            outcaeSureVo.setEndSureDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        page = outcaeSureService.selectByDomainWhere(page, outcaeSureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对外建安产值确权-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<OutcaeSureDomain> queryMyCheckDataByCondition(OutcaeSureVo outcaeSureVo,
                                                               ParamPage paramPage,
                                                               String monthStartDateStr,
                                                               String monthEndDateStr) {
        Page<OutcaeSureDomain> page = new Page<OutcaeSureDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (outcaeSureVo.getEndDate() != null) {
            outcaeSureVo.setEndDate(DateUtils.addDays(outcaeSureVo.getEndDate(), 1));
        }
        //查询时，确权开始日期
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            outcaeSureVo.setStartSureDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，确权结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            outcaeSureVo.setEndSureDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        outcaeSureVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = outcaeSureService.selectMyCheckByDomainWhere(page, outcaeSureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 对外建安产值确权-新增
     * @return
     */
    @RequestMapping("/add")
    public String toAdd(HttpServletRequest request) {
        String bindOrgId = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId"));
        String bindOrgName = String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgName"));
        String bindDeptId = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId"));
        String bindDeptName = String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptName"));
        request.setAttribute("bindOrgId", bindOrgId);
        request.setAttribute("bindOrgName", bindOrgName);
        request.setAttribute("bindDeptId", bindDeptId);
        request.setAttribute("bindDeptName", bindDeptName);
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        //返回所有项目合同中的项目名称
        List<ProContract> proContractList = proContractService.selectAllContracts(null, null);
        request.setAttribute("proContractList", proContractList);
        return "bpa/lay-outcaeSure-add";
    }

//    /**
//     * 根据项目合同中项目编号查询相关带入信息
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @RequestMapping("/getOutcaeSureByProCode")
//    @ResponseBody
//    public Map<String, Object> getOutcaeSureByProCode(HttpServletRequest request) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        String proCode = request.getParameter("proCodeAndCodes");
//        String[] split = proCode.split("\\_");
//        try {
//            //主表信息
//            ProContractDomain contractProCode = proContractService.selectByContractProCode(split[0]);
//            if (contractProCode != null) {
//                map.put("contractProCode", contractProCode);
//                //带出清单信息
//                ContractExt contractExt = contractExtService.selectByProCode(split[0]);
//                map.put("contractExt", contractExt);
//                if (contractExt != null) {
//                    List<ContractExtDetailAndSubDomain> selectDetailAndSubList = contractExtDetailService.selectDetailAndSub(
//                        contractExt.getId(), "1", null, null, null, "1");
//                    List<ContractExtDetailAndSubDomain> list = new ArrayList<>();
//                    for (ContractExtDetailAndSubDomain cSubDomain : selectDetailAndSubList) {
//                        //历史确权(下浮前)
//                        BigDecimal addupAmountTotalBefore = outcaeSureDetailService.selectAddupAmountBySubNameAndRefId(
//                            cSubDomain.getSubName(), split[1], cSubDomain.getTypeName());
//                        if (addupAmountTotalBefore == null) {
//                            cSubDomain.setAddupAmountTotalBefore(BigDecimal.ZERO);
//                        }
//                        cSubDomain.setAddupAmountTotalBefore(addupAmountTotalBefore);
//                        //历史确权(下浮后)
//                        BigDecimal addupAmountTotalAfter = outcaeSureDetailService.selectAddupAmountBySubNameAfter(
//                            cSubDomain.getSubName(), split[1], cSubDomain.getTypeName());
//                        if (addupAmountTotalAfter == null) {
//                            cSubDomain.setAddupAmountTotalAfter(BigDecimal.ZERO);
//                        }
//                        cSubDomain.setAddupAmountTotalAfter(addupAmountTotalAfter);
//                        list.add(cSubDomain);
//                    }
//                    map.put("selectDetailAndSubList", list);
//                    ContractExtInvest contractExtInvest = new ContractExtInvest();
//                    contractExtInvest.setContractExtId(contractExt.getId());
//                    List<ContractExtInvest> selectByEntityWhere = contractExtInvestService.selectByEntityWhere(contractExtInvest);
//                    if (selectByEntityWhere.size() > 0) {
//                        map.put("contractExtInvest", selectByEntityWhere.get(0));
//                    }
//                }
//                else {
//                    List<ProContractDetailAndSubDomain> selectDetailAndSubList = proContractDetailService.selectDetailAndSub(
//                        contractProCode.getId(), "1", null, null, null, "1");
//                    List<ProContractDetailAndSubDomain> list = new ArrayList<>();
//                    for (ProContractDetailAndSubDomain cSubDomain : selectDetailAndSubList) {
//                        //历史确权(下浮前)
//                        BigDecimal addupAmountTotalBefore = outcaeSureDetailService.selectAddupAmountBySubNameAndRefId(
//                            cSubDomain.getSubName(), split[1], cSubDomain.getTypeName());
//                        if (addupAmountTotalBefore == null) {
//                            cSubDomain.setAddupAmountTotalBefore(BigDecimal.ZERO);
//                        }
//                        cSubDomain.setAddupAmountTotalBefore(addupAmountTotalBefore);
//                        //历史确权(下浮后)
//                        BigDecimal addupAmountTotalAfter = outcaeSureDetailService.selectAddupAmountBySubNameAfter(
//                            cSubDomain.getSubName(), split[1], cSubDomain.getTypeName());
//                        if (addupAmountTotalAfter == null) {
//                            cSubDomain.setAddupAmountTotalAfter(BigDecimal.ZERO);
//                        }
//                        cSubDomain.setAddupAmountTotalAfter(addupAmountTotalAfter);
//                        list.add(cSubDomain);
//                    }
//                    map.put("selectDetailAndSubList", list);
//                    ProContractInvest proContractInvest = new ProContractInvest();
//                    proContractInvest.setProContractId(contractProCode.getId());
//                    List<ProContractInvest> entityWhere = proContractInvestService.selectByEntityWhere(proContractInvest);
//                    if (entityWhere.size() > 0) {
//                        map.put("proContractInvest", entityWhere.get(0));
//                    }
//                }
//            }
//        }
//        catch (Exception e) {
//            log.error("查询项目合同记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

    /**
     * 根据分部分项名称和refId查询月累金额
     * @param id
     * @return
     */
    @RequestMapping("/getAddupAmountBySubNameAndRefId")
    @ResponseBody
    public Map<String, Object> getAddupAmountBySubNameAndRefId(String subName,
                                                               String proCodeAndCodes,
                                                               String typeName) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isBlank(subName) || StringUtils.isBlank(proCodeAndCodes)
            || StringUtils.isBlank(typeName)) {
            flag = false;
        }
        else {
            try {
                String[] split = proCodeAndCodes.split("\\_");
                String refId = split[1];
                BigDecimal addupAmountTotal = outcaeSureDetailService.selectAddupAmountBySubNameAndRefId(
                    subName, refId, typeName);
                map.put("addupAmountTotal", addupAmountTotal);
            }
            catch (Exception e) {
                log.error("查询对外建安产值确权记录异常", e);
                flag = false;
            }
        }
        map.put("flag", flag);
        return map;
    }

    /**
     * 对外建安产值确权-新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(OutcaeSureAssist outcaeSureAssist)
        throws ParseException {

        //主表-对外建安产值确权
        outcaeSureAssist.setHrDeptId(outcaeSureAssist.getHrDeptid());
        outcaeSureAssist.setHrOrgId(outcaeSureAssist.getHrOrgid());
        outcaeSureAssist.setStaffName(outcaeSureAssist.getStaffname());
        outcaeSureAssist.setHrStaffApplyId(outcaeSureAssist.getHrStaffApplyid());
        String proCode = "";
        String code = "";
        String[] strCodes = outcaeSureAssist.getProCodeAndCodes().split("\\_");
        proCode = strCodes[0];
        code = strCodes[1];
        outcaeSureAssist.setRefContractName(strCodes[3]);
        outcaeSureAssist.setProCode(proCode);
        outcaeSureAssist.setRefExtId(code);
        Date month = DateUtils.parseDate(outcaeSureAssist.getSureMonthStr(), "yyyy-MM");
        outcaeSureAssist.setSureMonth(month);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        //添加项目名称
        outcaeSureAssist.setProName(strCodes[2]);
        outcaeSureAssist.setId(id);
        outcaeSureAssist.setAddBy(vo.getId());
        outcaeSureAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("bpa_outcae_sure");
        outcaeSureAssist.setCodes(codes);
        // 处理processId
        outcaeSureAssist.setProcessId(IdFactory.getUUIDUpper());
        outcaeSureAssist.setProcessStatus("1");//审批中
        outcaeSureAssist.setTypes("1");//PPP项目
        outcaeSureAssist.setUptBy(vo.getId());
        outcaeSureAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //新增对外建安产值确权分解子项
        List<OutcaeSureSub> outcaeSureSubs = new ArrayList<OutcaeSureSub>();
        //对外建安产值确权清单分解子项费用明细
        List<OutcaeSureDetail> outcaeSureDetails = new ArrayList<OutcaeSureDetail>();
        //子项名称
        String[] subNames = outcaeSureAssist.getSubNames();
        if (subNames != null && subNames.length > 0) {
            for (int i = 0; i < subNames.length; i++ ) {
                String subName = subNames[i];
                if (StringUtils.isEmpty(subName) && StringUtils.isEmpty(subName)) {
                    continue;
                }
                OutcaeSureSub outcaeSureSub = new OutcaeSureSub();
                String outcaeSureSubId = IdFactory.getUUIDUpper();//自动生成主键
                outcaeSureSub.setId(outcaeSureSubId);
                outcaeSureSub.setOutcaeSureId(id);
                outcaeSureSub.setSubName(subName);
                outcaeSureSub.setAddBy(vo.getId());
                Date addTime = top.infopub.mgr.util.DateUtils.addSeconds(
                    top.infopub.mgr.util.DateUtils.getCurrentDateTime(), i);
                outcaeSureSub.setAddTime(addTime);
                outcaeSureSubs.add(outcaeSureSub);
            }

            //费用大类
            String[] feeSmallTypes = outcaeSureAssist.getFeeSmallTypes();
            for (int j = 0; j < feeSmallTypes.length; j++ ) {
                String feeSmallType = feeSmallTypes[j];
                String itemNameSub = outcaeSureAssist.getItemNameSub()[j];
                String typeName = outcaeSureAssist.getTypeNames()[j];
                String orderIdx = outcaeSureAssist.getOrderIdxs()[j];
                String singleTotal = outcaeSureAssist.getSingleTotals()[j];
                String monthPreAmount = outcaeSureAssist.getMonthPreAmounts()[j];
                String monthAfterAmount = outcaeSureAssist.getMonthAfterAmounts()[j];
                String addupPreAmount = outcaeSureAssist.getAddupPreAmounts()[j];
                String addupAfterAmount = outcaeSureAssist.getAddupAfterAmounts()[j];
                String outDownRate = outcaeSureAssist.getOutDownRates()[j];
                String notDownAmount = outcaeSureAssist.getNotDownAmounts()[j];
                if (StringUtils.isEmpty(feeSmallType)) {
                    continue;
                }
                OutcaeSureDetail outcaeSureDetail = new OutcaeSureDetail();
                String outcaeSureDetailId = IdFactory.getUUIDUpper();//自动生成主键
                outcaeSureDetail.setId(outcaeSureDetailId);

                outcaeSureDetail.setSureSubId(itemNameSub);

                outcaeSureDetail.setOutcaeSureId(id);
                outcaeSureDetail.setOrderIdx(orderIdx);
                outcaeSureDetail.setTypeName(typeName);
                String[] split = orderIdx.split("\\.");
                if (split.length == 1) {
                    outcaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType));
                }
                else if (split.length == 2) {
                    String feeSmallType2 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    outcaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType2));
                }
                else if (split.length == 3) {
                    String feeSmallType3 = feeSmallType.charAt(0) + "" + feeSmallType.charAt(2);
                    outcaeSureDetail.setFeeSmallType(Integer.parseInt(feeSmallType3));
                }
                outcaeSureDetail.setSingleTotal(new BigDecimal(singleTotal));
                outcaeSureDetail.setMonthPreAmount(new BigDecimal(monthPreAmount));
                outcaeSureDetail.setMonthAfterAmount(new BigDecimal(monthAfterAmount));
                outcaeSureDetail.setAddupPreAmount(new BigDecimal(addupPreAmount));
                outcaeSureDetail.setAddupAfterAmount(new BigDecimal(addupAfterAmount));
                outcaeSureDetail.setOutDownRate(new BigDecimal(outDownRate));
                outcaeSureDetail.setNotDownAmount(new BigDecimal(notDownAmount));
                outcaeSureDetail.setAddBy(vo.getId());
                outcaeSureDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
                outcaeSureDetails.add(outcaeSureDetail);
            }

            for (OutcaeSureDetail outcaeSureDetail : outcaeSureDetails) {
                for (OutcaeSureSub outcaeSureSub : outcaeSureSubs) {
                    if (outcaeSureDetail.getSureSubId().equals(outcaeSureSub.getSubName())) {
                        outcaeSureDetail.setSureSubId(outcaeSureSub.getId());
                        break;
                    }
                }
            }
        }

        //上传附件
        //1=计算稿
        List<OutcaeSureAtt> outcaeSureAtts = new ArrayList<OutcaeSureAtt>();
        String[] fileAtt1 = outcaeSureAssist.getFileAtts1();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                OutcaeSureAtt outcaeSureAtt = createFileAtt(vo, id, file);
                outcaeSureAtt.setTypes("11");
                outcaeSureAtts.add(outcaeSureAtt);
            }
        }
        //2=签字扫描件
        String[] fileAtt2 = outcaeSureAssist.getFileAtts2();
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                OutcaeSureAtt outcaeSureAtt = createFileAtt(vo, id, file);
                outcaeSureAtt.setTypes("12");
                outcaeSureAtts.add(outcaeSureAtt);
            }
        }
        //3=其他附件
        String[] fileAtt3 = outcaeSureAssist.getFileAtts3();
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                OutcaeSureAtt outcaeSureAtt = createFileAtt(vo, id, file);
                outcaeSureAtt.setTypes("4");
                outcaeSureAtts.add(outcaeSureAtt);
            }
        }

        outcaeSureService.saveOutcaeSure(outcaeSureAssist, outcaeSureSubs, outcaeSureDetails,
            outcaeSureAtts);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "商务管理 -确权支付-对外建安产值确权：项目名="
                                                              + outcaeSureAssist.getProName(),
            null);
        return RspVo.getSuccessResponse();
    }

    private OutcaeSureAtt createFileAtt(AuthorizationVO vo, String outcaeSureId, String file) {
        //资料信息数组
        String[] fileAttStr = file.split("\\|");
        OutcaeSureAtt fileAtt = new OutcaeSureAtt();
        if(StringUtils.isNotEmpty(fileAttStr[2])){
            fileAtt.setId(fileAttStr[2]);
        }else{
            fileAtt.setId(IdFactory.getUUIDSerialNumber());
        }
        fileAtt.setOutcaeSureId(outcaeSureId);//项目id
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
            fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 上传附件
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/uploadAtt")
    @ResponseBody
    public RspVo updateAtt(HttpServletRequest request) {
        List<String> returnFileStrList = new ArrayList<>();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
        if (fileMap != null) {
            for (String name : fileMap.keySet()) {
                MultipartFile file = fileMap.get(name);
                //资料类型
                String types = request.getParameter("types");
                if (StringUtils.isEmpty(types)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
                }
                //上传文件
                String fileId = IdFactory.getUUIDSerialNumber();
                //服务器资源路径
                String serverPath = "outcaeSure";
                String filePath = FileHelper.upload(file, serverPath);
                returnFileStrList.add("|" + file.getOriginalFilename().trim() + "|" + fileId + "|"
                                      + filePath);
                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传确权支付-对外建安产值确权附件-文件名:"
                                                                      + filePath + "，文件路径:"
                                                                      + filePath, null);
            }
        }
        return RspVo.getSuccessResponse(returnFileStrList);
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 对外建安产值确权id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        OutcaeSureVo outcaeSureVo = outcaeSureService.queryById(id);
        OrgInfo orgInfo = new OrgInfo();
        orgInfo.setProjectCode(outcaeSureVo.getProCode());
        orgInfo.setState(1);
        List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
        if (orgInfos.size() > 0) {
            outcaeSureVo.setOrgProName(orgInfos.get(0).getCompName());
            outcaeSureVo.setHrOrgProId(orgInfos.get(0).getId());
            OrgInfo parentOrgInfo = new OrgInfo();
            parentOrgInfo.setId(orgInfos.get(0).getParentId());
            parentOrgInfo.setState(1);
            List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
            if (parentOInfos.size() > 0) {
                outcaeSureVo.setOrgAreName(parentOInfos.get(0).getCompName());
                outcaeSureVo.setHrOrgAreaId(parentOInfos.get(0).getId());
            }
        }
        for (OutcaeSureSubVo outcaeSureSub : outcaeSureVo.getOutcaeSureSubVos()) {
            // 其中： 对内下浮率如下 
            // 1. 带入项目【分包合同】（可能需查多个合同）子项对应{对内下浮率}
            // 2. 带入项目【内部协议】子项对应{对内下浮率}
            // 3. 若无对应子项，为0
            Map<String, Object> subpackCotWhere = Maps.newHashMap();
            subpackCotWhere.put("proCode", outcaeSureVo.getProCode());
            subpackCotWhere.put("subName", outcaeSureSub.getSubName());
            // 查询项目【分包合同】 子项对应{对内下浮率}
            BigDecimal rateFromSubpackCot = subpackCotDetailService.selectRateFromSubpackCot(subpackCotWhere);
            if (null == rateFromSubpackCot) {
                Map<String, Object> innerAgrtWhere = Maps.newHashMap();
                innerAgrtWhere.put("proCode", outcaeSureVo.getProCode());
                innerAgrtWhere.put("subName", outcaeSureSub.getSubName());
                // 查询项目【内部协议】子项对应{对内下浮率}
                BigDecimal rateFromInnerAgrt = innerAgrtDetailService.selectRateFromInnerAgrt(innerAgrtWhere);
                if (null == rateFromInnerAgrt) {
                    outcaeSureSub.setInDownRates(BigDecimal.ZERO);
                }
                else {
                    outcaeSureSub.setInDownRates(rateFromInnerAgrt);
                }
            }
            else {
                outcaeSureSub.setInDownRates(rateFromSubpackCot);
            }
        }
        request.setAttribute("modelDetail", outcaeSureVo);
        map.put("outcaeSure", outcaeSureVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, outcaeSureVo.getProcessId());
        return new ModelAndView("bpa/lay-outcaeSure-detail", map);
    }

    /**
     * 对外建安产值确权-复制单据
     * @return
     */
    @RequestMapping("/toCopy")
    public String toCopy(HttpServletRequest request) {
        String id = request.getParameter("id");
        OutcaeSureVo outcaeSureVo = outcaeSureService.queryById(id);
        request.setAttribute("outcaeSure", outcaeSureVo);
        return "bpa/lay-outcaeSure-copy";
    }
    /**
     * 根据对外建安确权id查询相关带入信息
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getOutcaeSureById")
    @ResponseBody
    public Map<String, Object> getOutcaeSureById(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        String sguid = request.getParameter("sguid");
        try {
            OutcaeSureVo outcaeSureVo = outcaeSureService.queryById(sguid);
            map.put("outcaeSureVo", outcaeSureVo);
        }catch(Exception e){
            log.error("查询对外建安确权记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map; 

    }
}
